package com.doge.calcalbum.business.trash

import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import androidx.activity.viewModels
import androidx.core.view.isVisible
import androidx.recyclerview.widget.GridLayoutManager
import com.doge.calcalbum.R
import com.doge.calcalbum.business.ITEM_CHECK
import com.doge.calcalbum.core.BaseActivity
import com.doge.calcalbum.core.base.adapters.ITEM_CLICKED
import com.doge.calcalbum.core.base.adapters.ITEM_LONG_CLICKED
import com.doge.calcalbum.databinding.ActivityTrashBinding
import com.doge.calcalbum.db.model.Entry
import com.doge.calcalbum.utils.dp
import com.doge.calcalbum.utils.setOnClickCallback
import com.doge.calcalbum.utils.toast
import com.doge.calcalbum.widgets.GridItemDecoration
import com.doge.calcalbum.widgets.viewer.TransitionViewsRef

class TrashActivity : BaseActivity() {
    private val binding by lazy { ActivityTrashBinding.inflate(layoutInflater) }
    private val viewModel by viewModels<TrashViewModel> { TrashViewModelFactory() }
    private val adapter by lazy { TrashEntryAdapter() }
    private var dataList: List<Entry>? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        initialViews()
        binding.recyclerView.itemAnimator = null
        val itemDecoration = object : GridItemDecoration(2.dp, Color.TRANSPARENT) {
            override fun getItemSidesIsHaveOffsets(itemPosition: Int) =
                arrayOf(itemPosition % 4 != 0, true, itemPosition % 4 != 3, true)
        }
        binding.recyclerView.addItemDecoration(itemDecoration)
        binding.recyclerView.layoutManager = GridLayoutManager(binding.recyclerView.context, 4)
        binding.recyclerView.adapter = adapter
        adapter.setListener(::handleAdapterCallback)
        viewModel.entryList.observe(this) {
            dataList = it
            binding.empty.isVisible = it.isNullOrEmpty()
            adapter.submitList(it)
        }
    }

    private fun handleAdapterCallback(action: String, item: Any?) {
        when (action) {
            ITEM_CLICKED -> showViewer(item as? Entry?)
            ITEM_LONG_CLICKED -> animToEditMode(item as? Entry?)
            ITEM_CHECK -> adapter.switch(item as? Entry?)
        }
    }

    private fun showViewer(clicked: Entry?) {
        if (clicked == null) return
//        ViewerHelper.provideImageViewerBuilder(
//            this, TransitionViewsRef.KEY_TRASH, clicked, dataList ?: listOf()
//        ).show()
        // 回收站禁止查看大图
    }

    private fun animToEditMode(entry: Entry? = null) {
        binding.editCancel.isVisible = true
        binding.selectAll.isVisible = true
        binding.bottomMenu.isVisible = true
        binding.edit.isVisible = false
        binding.toolbar.binding.pageBack.isVisible = false
        adapter.enterEdit(entry)
    }

    private fun animToNormalMode() {
        binding.editCancel.isVisible = false
        binding.selectAll.isVisible = false
        binding.bottomMenu.isVisible = false
        binding.edit.isVisible = true
        binding.toolbar.binding.pageBack.isVisible = true
        adapter.exitEdit()
    }


    private fun initialViews() {
        binding.edit.setOnClickCallback {
            animToEditMode()
        }
        binding.editCancel.setOnClickCallback {
            animToNormalMode()
        }
        binding.selectAll.setOnClickCallback {
            adapter.switchAll()
        }
        binding.delete.setOnClickCallback {
            val selected = adapter.selected
            if (selected.isEmpty()) {
                toast(R.string.please_select_entry)
            } else {
                viewModel.delete(selected)
            }
        }
        binding.move.setOnClickCallback {
            val selected = adapter.selected
            if (selected.isEmpty()) {
                toast(R.string.please_select_entry)
            } else {
                viewModel.recover(selected)
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        TransitionViewsRef.releaseTransitionViewRef(TransitionViewsRef.KEY_TRASH)
    }

    companion object {
        fun start(context: Context) {
            context.startActivity(Intent(context, TrashActivity::class.java))
        }
    }
}